
// Source File Name:   CDImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

//import oracle.xml.parser.v2.XMLDocument;
//import oracle.xml.parser.v2.XMLElement;
//import oracle.xml.parser.v2.XMLNode;

import com.sunwayhorizo.health.ets.base.persistent.EtCodingSchemes;
import com.sunwayhorizo.health.ets.base.service.ETSService;
import com.sunwayhorizo.health.ets.exceptions.ETSException;
import com.sunwayhorizo.health.ets.exceptions.ETSNotFoundException;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CD;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.ED;
import com.sunwayhorizo.health.hl7.datatypes.LIST_CR;
import com.sunwayhorizo.health.hl7.datatypes.SET_CD;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;
import com.sunwayhorizo.health.hl7.datatypes.UID;


// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            ANYImpl, BLImpl, CSImpl, EDImpl, 
//            LIST_CRAdapterImpl, NullFlavorImpl, OIDImpl, SET_CDImpl, 
//            STImpl, TYPEImpl
/**
 * =============================================================
 * @ClassName dd <br>
 * @PackageName com.sunwayhorizo.health.hl7.datatypes.common <br>
 * @Description 去掉熟悉mCodeSystemName，增加mCodeSystemEnName，
 * mCodeSystemChName，mCodeName三个属性，修改与属性相关代码 <br>
 * @Author jinzw <br>
 * @Date 2010-11-15 <br>
 * @Version 1.0 <br>
 * @ModifiedDesc <br>
 * @ModifiedDate <br>
 * @ModifiedBy <br>
 * @Email tanll@sunwaysoft.com.cn <br>
 * @Copyright 2010-2020 北京三维海容 All Rights Reserved. <br>
 * =============================================================
 */
public class CDImpl extends ANYImpl
    implements CD
{

    public static final String RCS_ID = "$Header: CDImpl.java 1.0 2006/09/16 10:39:32 mmoores noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header: CDImpl.java 115.55 2005/09/16 10:39:32 mmoores noship $", "com.sunwayhorizo.health.hl7.datatypes");
    private static final String CODE = "code";
    private static final String CODESYSTEMNAME = "CodeSystemName";
    private static final String CODESYSTEM = "codeSystem";
    private static final String CODESYSTEMVERSION = "codeSystemVersion";
    private static final String DISPLAYNAME = "displayName";
    private static final String ORIGINALTEXT = "originalText";
    private static final String QUALIFIER = "qualifier";
    private static final String TRANSLATION = "translation";
    private ST mCode; //编码
    private UID mCodeSystem;
//    private ST mCodeSystemName;
    private ST mCodeSystemEnName; //ets术语字典英文名
    private ST mCodeSystemChName; //ets术语字典中文名
    private ST mCodeName; //ets术语名称
    private ST mCodeSystemVersion;
    private ST mDisplayName;
    private ED mOriginalText;
    private LIST_CR mQualifier;
    private SET_CD mTranslation;
    private static final CD nullNI = new CDImpl(CSImpl.valueOf("NI"));
    private static final CD nullOTH = new CDImpl(CSImpl.valueOf("OTH"));
    private static final CD nullNINF = new CDImpl(CSImpl.valueOf("NINF"));
    private static final CD nullPINF = new CDImpl(CSImpl.valueOf("PINF"));
    private static final CD nullUNK = new CDImpl(CSImpl.valueOf("UNK"));
    private static final CD nullASKU = new CDImpl(CSImpl.valueOf("ASKU"));
    private static final CD nullNAV = new CDImpl(CSImpl.valueOf("NAV"));
    private static final CD nullMSK = new CDImpl(CSImpl.valueOf("MSK"));
    private static final CD nullNA = new CDImpl(CSImpl.valueOf("NA"));
    private static final CD nullNASK = new CDImpl(CSImpl.valueOf("NASK"));
    
    /**
     * 增加构造方法
     * 为了避免和CDImpl(CS cs)构造方法重载冲突，故加了一个参数
     * ST mCode 编码
     * String flag，flag为null的情况下不做任何处理
     */
    protected CDImpl(String flag,ST code){
    		mCode = code;
    }
    
    
    /**
     *  增加构造方法，传递六个参数
     *  ST mCode;  //编码
     *  UID mCodeSystem;
     *  ST mCodeSystemEnName; //ets术语字典英文名
     *  ST mCodeSystemChName; //ets术语字典中文名
     *  ST mCodeName; //ets术语名称
     *  ST mCodeSystemVersion; 版本
     *  
     *  added by : lordtan
     *  date : 2010-11-19
     */
    protected CDImpl(ST code,UID codeSystem,ST codeSystemEnName,ST codeSystemChName,ST codeName,ST codeSystemVersion){
    	mCode = code;
    	mCodeSystem = codeSystem;
    	mCodeSystemEnName = codeSystemEnName;
    	mCodeSystemChName = codeSystemChName;
    	mCodeName = codeName;
    	mCodeSystemVersion = codeSystemVersion;
    }
    

//    protected CDImpl(ETSService etsservice, ST st, UID uid, ST st1, ST st2, ST st3, ED ed, 
//            LIST_CR list_cr, SET_CD set_cd)
//        throws ETSException
//    {
//        if(st == null || st.isNull().isTrue())
//            throw ANYImpl.newNullException("CD", "constructor", "code");
//        if((uid == null || uid.isNull().isTrue()) && (st1 == null || st1.isNull().isTrue()))
//            throw ANYImpl.newNullException("CD", "constructor", "codeSystem");
//        mCode = st;
//        if(uid == null || uid.isNull().isTrue())
//            mCodeSystem = getCodeSystemFromName(etsservice, st1);
//        else
//            mCodeSystem = uid;
//        mCodeSystemName = st1;
//        mCodeSystemVersion = st2;
//        mDisplayName = st3;
//        mOriginalText = ed;
//        mQualifier = list_cr;
//        mTranslation = set_cd;
//    }
    
    
    protected CDImpl(ETSService etsservice, ST st, UID uid, ST st1, ST st2, ST st3, ST st4, ST st5, ED ed, 
            LIST_CR list_cr, SET_CD set_cd)
        throws ETSException
    {
        if(st == null || st.isNull().isTrue())
            throw ANYImpl.newNullException("CD", "constructor", "code");
        if((uid == null || uid.isNull().isTrue()) && (st1 == null || st1.isNull().isTrue()))
            throw ANYImpl.newNullException("CD", "constructor", "codeSystem");
        mCode = st;
        if(uid == null || uid.isNull().isTrue())
            mCodeSystem = getCodeSystemFromName(etsservice, st1);
        else
            mCodeSystem = uid;
        mCodeSystemEnName = st1;
        mCodeSystemChName = st2;
        mCodeName = st3;
        mCodeSystemVersion = st4;
        mDisplayName = st5;
        mOriginalText = ed;
        mQualifier = list_cr;
        mTranslation = set_cd;
    }
    
//    protected CDImpl(ST st, UID uid, ST st1, ST st2, ST st3, ED ed, LIST_CR list_cr, 
//            SET_CD set_cd)
//    {
//        if(st == null || st.isNull().isTrue())
//            throw ANYImpl.newNullException("CD", "constructor", "code");
//        if(uid == null || uid.isNull().isTrue())
//        {
//            throw ANYImpl.newNullException("CD", "constructor", "codeSystem");
//        } else
//        {
//            mCode = st;
//            mCodeSystem = uid;
//            mCodeSystemName = st1;
//            mCodeSystemVersion = st2;
//            mDisplayName = st3;
//            mOriginalText = ed;
//            mQualifier = list_cr;
//            mTranslation = set_cd;
//            return;
//        }
//    }

    protected CDImpl(ST st, UID uid,ST st1, ST st2, ST st3, ST st4, ST st5, ED ed, LIST_CR list_cr, 
            SET_CD set_cd)
    {
        if(st == null || st.isNull().isTrue())
            throw ANYImpl.newNullException("CD", "constructor", "code");
        if(uid == null || uid.isNull().isTrue())
        {
            throw ANYImpl.newNullException("CD", "constructor", "codeSystem");
        } else
        {
            mCode = st;
            mCodeSystem = uid;
            mCodeSystemEnName = st1;
            mCodeSystemChName = st2;
            mCodeName = st3;
            mCodeSystemVersion = st4;
            mDisplayName = st5;
            mOriginalText = ed;
            mQualifier = list_cr;
            mTranslation = set_cd;
            return;
        }
    }

    protected CDImpl(CS cs)
    {
        super(true, cs);
    }

    private UID getCodeSystemFromName(ETSService etsservice, ST st)
        throws ETSException
    {
    	EtCodingSchemes codingscheme = etsservice.getCodingScheme(st.stringValue());
        if(codingscheme != null && codingscheme.getCodingschemeOid() != null)
            return OIDImpl.valueOf(codingscheme.getCodingschemeOid());
        else
            throw ANYImpl.newNullException("CD", "getCodeSystemFromName", "OID");
    }

    public ST code()
    {
        if(isNull().isTrue())
            return STImpl.nullST(NullFlavorImpl.NI);
        else
            return mCode;
    }

    public ST displayName()
    {
        if(isNull().isTrue())
            return STImpl.nullST(NullFlavorImpl.NI);
        else
            return mDisplayName;
    }

    public UID codeSystem()
    {
        if(isNull().isTrue())
            return OIDImpl.nullOID(NullFlavorImpl.NI);
        else
            return mCodeSystem;
    }
    
    public ST codeSystemVersion()
    {
        if(isNull().isTrue())
            return STImpl.nullST(NullFlavorImpl.NI);
        else
            return mCodeSystemVersion;
    }

    public ED originalText()
    {
        if(isNull().isTrue())
            return EDImpl.nullED(NullFlavorImpl.NI);
        else
            return mOriginalText;
    }

    public LIST_CR qualifier()
    {
        throw ANYImpl.newUnsupportedException("CD", "qualifier");
    }

    public SET_CD translation()
    {
        throw ANYImpl.newUnsupportedException("CD", "translation");
    }

    public BL implies(CD cd)
    {
        throw ANYImpl.newUnsupportedException("CD", "implies");
    }

    public int hashCode()
    {
        int i = 17;
        if(isNull().isFalse())
        {
            i = 37 * i + code().hashCode();
            i = 37 * i + codeSystem().hashCode();
        }
        return i;
    }

    public BL equal(ANY any)
    {
        if(any.dataType().equal(com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.CD).isTrue())
        {
            if(any.isNull().isTrue())
            {
                return BLImpl.FALSE;
            } else
            {
                CD cd = (CD)any;
                BL bl = code().equal(cd.code()).and(codeSystem().equal(cd.codeSystem()));
                return bl;
            }
        } else
        {
            return BLImpl.FALSE;
        }
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.CD;
    }

    /**
     * 增加参数，使之适应修改之后的构造方法
     * jinzw
     * 2010-11-15
     * @param s
     * @param s1  用来构建 com.sunwayhorizo.health.hl7.datatypes.OID
     * @param s2  用来构建 mCodeSystemEnName
     * @param s3  用来构建 mCodeSystemChName
     * @param s4  用来构建 mCodeName
     * @param s5  用来构建
     * @param ed
     * @return
     */
    public static CD valueOf(String s, String s1, String s2, String s3, String s4, String s5, ED ed)
    {
        ST st = s == null ? STImpl.nullST(NullFlavorImpl.NI) : STImpl.valueOf(s);
        com.sunwayhorizo.health.hl7.datatypes.OID oid = s1 == null ? OIDImpl.nullOID(NullFlavorImpl.NI) : OIDImpl.valueOf(s1);
        ST st1 = s2 == null ? STImpl.nullST(NullFlavorImpl.NI) : STImpl.valueOf(s2);
        ST st2 = s3 == null ? STImpl.nullST(NullFlavorImpl.NI) : STImpl.valueOf(s3);
        ST st3 = s4 == null ? STImpl.nullST(NullFlavorImpl.NI) : STImpl.valueOf(s4);
        ST st4 = s5 == null ? STImpl.nullST(NullFlavorImpl.NI) : STImpl.valueOf(s5);
        ST st5 = STImpl.nullST(NullFlavorImpl.NI);
        LIST_CR list_cr = LIST_CRAdapterImpl.nullLIST_CR(NullFlavorImpl.NI);
        SET_CD set_cd = SET_CDImpl.nullSET_CD(NullFlavorImpl.NI);
        return new CDImpl(st, oid, st1, st2, st3,st4, st5, ed, list_cr, set_cd);
    }

    /**
     * 增加参数，使之适应修改后的构造方法
     * jinzw
     * 2010-11-15
     * @param etsservice
     * @param st
     * @param uid
     * @param st1 对应com.sunwayhorizo.health.hl7.datatypes.common.CDImpl中的mCodeSystemEnName
     * @param st2 对应com.sunwayhorizo.health.hl7.datatypes.common.CDImpl中的mCodeSystemChName
     * @param st3 对应com.sunwayhorizo.health.hl7.datatypes.common.CDImpl中的mCodeName
     * @param st4
     * @param st5
     * @param ed
     * @param list_cr
     * @param set_cd
     * @return
     * @throws ETSException
     */
    public static CD valueOf(ETSService etsservice, ST st, UID uid, ST st1, ST st2, ST st3,ST st4,ST st5, ED ed, LIST_CR list_cr, 
            SET_CD set_cd)
        throws ETSException
    {
        return new CDImpl(etsservice, st, uid, st1, st2, st3,st4,st5, ed, list_cr, set_cd);
    }

    public static CD valueOf(ETSService etsservice, String s)
        throws ETSException
    {
        String as[] = {
            s
        };
        SET_CD set_cd = SET_CDImpl.newSET_CD(etsservice, as);
        CD acd[] = (CD[])set_cd.toSet().toArray(new CD[0]);
        if(acd != null && acd.length != 0)
            return acd[0];
        else
            throw new ETSNotFoundException("ETS ID passed in is not valid");
    }

    /**
     * 修改返回值错误
     * jinzw
     * 2010-11-15
     * @param s
     * @param s1
     * @return
     */
    public static CD valueOf(String s, String s1)
    {
        ST st = STImpl.valueOf(s);
        com.sunwayhorizo.health.hl7.datatypes.OID oid = OIDImpl.valueOf(s1);
        ST st1 = STImpl.nullST(NullFlavorImpl.NI);
        return new CDImpl(st, oid, st1, st1, st1,st1,st1, EDImpl.nullED(NullFlavorImpl.NI), LIST_CRAdapterImpl.nullLIST_CR(NullFlavorImpl.NI), SET_CDImpl.nullSET_CD(NullFlavorImpl.NI));
    }
    
    public static CD valueOf(String s)
    {
       CS cs=CSImpl.valueOf(s);
        return new CDImpl(cs);
    }
    
    public static CD nullCD()
    {
        return new CDImpl(null);
    }

    /**
     * 修改内容：增加mCodeSystemEnName，mCodeSystemChName，mCodeName这三个属性的xml数据内容
     * 修改人：jinzw
     * 修改时间：2010-11-15
     */
//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = (XMLElement)xmldocument.createElement(s);
//            if(code() != null && code().isNull().isFalse())
//                xmlelement.setAttribute("code", code().stringValue());
//            if(codeSystemEnName() != null && codeSystemEnName().isNull().isFalse())
//                xmlelement.setAttribute("codeSystemEnName", codeSystemEnName().stringValue());
//            if(codeSystemChName() != null && codeSystemChName().isNull().isFalse())
//                xmlelement.setAttribute("codeSystemChName", codeSystemChName().stringValue());
//            if(codeName() != null && codeName().isNull().isFalse())
//                xmlelement.setAttribute("codeName", codeName().stringValue());
//            if(codeSystem() != null && codeSystem().isNull().isFalse())
//                xmlelement.setAttribute("codeSystem", codeSystem().stringValue());
//            if(codeSystemVersion() != null && codeSystemVersion().isNull().isFalse())
//                xmlelement.setAttribute("codeSystemVersion", codeSystemVersion().stringValue());
//            if(displayName() != null && displayName().isNull().isFalse())
//                xmlelement.setAttribute("displayName", displayName().stringValue());
//            if(originalText() != null && originalText().isNull().isFalse())
//                xmlelement.appendChild(((EDImpl)originalText()).writeXML(xmldocument, "originalText")[0]);
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }

    /**
     * 增加mCodeSystemEnName，mCodeSystemChName，mCodeName
     * 修改人：jinzw
     * 修改时间：2010-11-15
     */
    public String toString()
    {
        String s = null;
        if(isNull().isFalse())
        {
            boolean flag = mCode != null && mCode.isNull().isFalse();
            boolean flag1 = mCodeSystem != null && mCodeSystem.isNull().isFalse();
            boolean flag2 = mCodeSystemEnName != null && mCodeSystemEnName.isNull().isFalse();
            boolean flag3 = mCodeSystemChName != null && mCodeSystemChName.isNull().isFalse();
            boolean flag4 = mCodeName != null && mCodeName.isNull().isFalse();
            s = (flag ? mCode.toString() : "null") + " [" + (flag1 ? mCodeSystem.toString() : "null") + (flag2 ? "/" + mCodeSystemEnName.toString() : "") + 
            (flag3 ? "/" + mCodeSystemChName.toString() : "") + (flag4 ? "/" + mCodeName.toString() : "") + "]";
        } else
        {
            s = "<Null:" + nullFlavor().toString() + ">";
        }
        return s;
    }

    public static CD nullCD(CS cs)
    {
        String s = null;
        if(cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if(s.equals("NI"))
            return nullNI;
        if(s.equals("OTH"))
            return nullOTH;
        if(s.equals("NINF"))
            return nullNINF;
        if(s.equals("PINF"))
            return nullPINF;
        if(s.equals("UNK"))
            return nullUNK;
        if(s.equals("ASKU"))
            return nullASKU;
        if(s.equals("NAV"))
            return nullNAV;
        if(s.equals("MSK"))
            return nullMSK;
        if(s.equals("NA"))
            return nullNA;
        if(s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }

	/* (non-Javadoc)
	 * @see com.sunwayhorizo.health.hl7.datatypes.CD#codeSystemEnName()
	 */
	public ST codeSystemEnName() {
		if(isNull().isTrue())
            return STImpl.nullST(NullFlavorImpl.NI);
        else
            return this.mCodeSystemEnName;
	}

	/* (non-Javadoc)
	 * @see com.sunwayhorizo.health.hl7.datatypes.CD#codeSystemChName()
	 */
	public ST codeSystemChName() {
		if(isNull().isTrue())
            return STImpl.nullST(NullFlavorImpl.NI);
        else
            return this.mCodeSystemChName; 
	}

	/* (non-Javadoc)
	 * @see com.sunwayhorizo.health.hl7.datatypes.CD#codeName()
	 */
	public ST codeName() {
		if(isNull().isTrue())
            return STImpl.nullST(NullFlavorImpl.NI);
        else
            return this.mCodeName;
	}


}
